//
//  NewAAlertView.m
//  HiCar
//
//  Created by huachun on 2019/4/17.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "NewAAlertView.h"

@interface NewAAlertView (){
    NSInteger Heretype;
}

@property (nonatomic, strong) UIImageView *Topimg;
@property (nonatomic, strong) UIView      *Middleview;

@property (nonatomic, strong) UILabel     *Tittle;
@property (nonatomic, strong) UILabel     *Info;

@property (nonatomic, strong) UIButton    *Cbutton;
@property (nonatomic, strong) UIButton    *Gobutton;

@end

@implementation NewAAlertView

+ (NewAAlertView *)Show:(NSInteger)type{
    NewAAlertView *showview     = [[NewAAlertView alloc] initWithFrame:CGRectMake((RECRWITCH-255)/2, -RECERHEIGHT, 255, 350) type:type];
    showview.backgroundColor = [UIColor clearColor];
    showview.bg_button                 = [[UIButton alloc] init];
    showview.bg_button.frame           = CGRectMake(0, 0, RECRWITCH, RECERHEIGHT);
    showview.bg_button.backgroundColor = [UIColor blackColor];
    showview.bg_button.alpha           = 0.0;
    [showview.bg_button addTarget:showview action:@selector(show_hide:) forControlEvents:UIControlEventTouchUpInside];
    
    [[UIApplication sharedApplication].keyWindow addSubview:showview.bg_button];
    [[UIApplication sharedApplication].keyWindow addSubview:showview];
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        showview.bg_button.alpha = 0.2;
        showview.frame=CGRectMake((RECRWITCH-255)/2, (RECERHEIGHT-350)/2, 255, 350);
    } completion:^(BOOL finished) {
        
    }];
    
    return showview;
}

-(void)show_hide:(NewAAlertView *)sender{
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.bg_button.alpha = 0.0;
        self.frame           = CGRectMake((RECRWITCH-255)/2, RECERHEIGHT, 255, 350);
    } completion:^(BOOL finished) {
        
    }];
}

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type{
    if (self=[super initWithFrame:frame]) {
        
        Heretype = type;
        
        _Topimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 130)];
        [self addSubview:_Topimg];
        
        _Middleview = [[UIView alloc] initWithFrame:CGRectMake(0, 120, frame.size.width, 140)];
        _Middleview.backgroundColor=[UIColor whiteColor];
        [self addSubview:_Middleview];
        
        _Tittle = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, 30)];
        _Tittle.font=[UIFont systemFontOfSize:18];
        _Tittle.textAlignment=NSTextAlignmentCenter;
        [_Middleview addSubview:_Tittle];
        
        _Info = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 30)];
        _Info.font=[UIFont systemFontOfSize:14];
        _Info.textAlignment=NSTextAlignmentCenter;
        _Info.textColor=[UIColor grayColor];
        [_Middleview addSubview:_Info];
        
        _Gobutton = [UIButton new];
        _Gobutton.frame=CGRectMake(140, 85, 90, 35);
        _Gobutton.layer.cornerRadius  = 17.5;
        _Gobutton.layer.masksToBounds = YES;
        [_Gobutton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_Middleview addSubview:_Gobutton];
        [_Gobutton addTarget:self action:@selector(go_done:) forControlEvents:UIControlEventTouchUpInside];
        [_Gobutton setBackgroundImage:[JonesClass createImageWithColor:NavItemTitleColor] forState:UIControlStateNormal];
        [_Gobutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _Cbutton = [UIButton new];
        _Cbutton.frame=CGRectMake(25, 85, 90, 35);
        _Cbutton.layer.cornerRadius  = 17.5;
        _Cbutton.layer.masksToBounds = YES;
        [_Cbutton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_Cbutton addTarget:self action:@selector(hide_done) forControlEvents:UIControlEventTouchUpInside];
        [_Middleview addSubview:_Cbutton];
        
        UIBezierPath *maskPathimg = [UIBezierPath bezierPathWithRoundedRect:_Topimg.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayerimg = [[CAShapeLayer alloc] init];
        maskLayerimg.frame = _Topimg.bounds;
        maskLayerimg.path = maskPathimg.CGPath;
        _Topimg.layer.mask = maskLayerimg;
        
        if (type==0) {
            _Topimg.contentMode=UIViewContentModeScaleToFill;
            _Topimg.image=[UIImage imageNamed:@"alertding.png"];
            _Tittle.text=@"您尚未开启定位";
            _Info.text=@"开启定位可以偶遇车友哦!";
            _Info.font=[UIFont systemFontOfSize:14];
            [_Cbutton setTitle:@"下次再说" forState:UIControlStateNormal];
            _Cbutton.layer.borderColor=[UIColor grayColor].CGColor;
            _Cbutton.layer.borderWidth=1.0;
            [_Cbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            
            [_Gobutton setTitle:@"前往设置" forState:UIControlStateNormal];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_Middleview.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = _Middleview.bounds;
            maskLayer.path = maskPath.CGPath;
            _Middleview.layer.mask = maskLayer;
        }else if (type==2){
            _Topimg.contentMode=UIViewContentModeScaleToFill;
            _Topimg.image=[UIImage imageNamed:@"alerttixing.png"];
            _Tittle.text=@"您尚未开启消息通知";
            _Info.text=@"立即开启消息通知,以免错过最新消息哦!";
            _Info.font=[UIFont systemFontOfSize:13];
            [_Cbutton setTitle:@"下次再说" forState:UIControlStateNormal];
            _Cbutton.layer.borderColor=[UIColor grayColor].CGColor;
            _Cbutton.layer.borderWidth=1.0;
            [_Cbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            
            [_Gobutton setTitle:@"前往设置" forState:UIControlStateNormal];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_Middleview.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = _Middleview.bounds;
            maskLayer.path = maskPath.CGPath;
            _Middleview.layer.mask = maskLayer;
        }
    }
    return self;
}

- (void)go_done:(UIButton *)sender{
    [self show_hide:self];
    
    if (Heretype==1){

    }else if (Heretype==2||Heretype==0){
        UIApplication *application = [UIApplication sharedApplication];
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([application canOpenURL:url]) {
            if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                if (@available(iOS 10.0, *)) {
                    [application openURL:url options:@{} completionHandler:nil];
                } else {
                    // Fallback on earlier versions
                }
            } else {
                [application openURL:url];
            }
        }
    }
}

-(void)hide_done{
    [self show_hide:self];
}

@end
